package br.com.gscorp.bankLion.utils;

import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;

import br.com.gscorp.bankLion.security.BankLionUserDetails;

public class MockSecurityContext implements SecurityContext {

	private static final long serialVersionUID = 3425893751915351844L;

	private Authentication authentication;

	private MockSecurityContext(Authentication authentication) {
		super();
		this.authentication = authentication;
	}

	@Override
	public Authentication getAuthentication() {
		return this.authentication;
	}

	@Override
	public void setAuthentication(Authentication authentication) {
		this.authentication = authentication;
	}

	public static MockHttpSession session(BankLionUserDetails details) {

		UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken(
				details, details.getPassword(), details.getAuthorities());

		MockHttpSession mockHttpSession = new MockHttpSession();
		mockHttpSession
				.setAttribute(
						HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
						new MockSecurityContext(principal));
		return mockHttpSession;
	}

}
